﻿using MB.RuleBase.Common;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Business
{
    public class BLmlocationgoods
    {


        public bool Lmlocationgoods_warehouse(string locationcode)
        {
            try
            {

                bool result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteScalar<int>("Lmlocationgoods", "warehouse", locationcode) > 0;
               return result;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        
        }

        public List<Lmlocationgoods> List_Lmlocationgoods(string locationcode)
        {
            try
            {
                List<Lmlocationgoods> list = new List<Lmlocationgoods>();
                list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Lmlocationgoods>("Lmlocationgoods", "GetLmlocationGoods", locationcode);
                return list;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }

        }
        public  LMLocationModel GetLMLocationInfoByCode(string locationcode)
        {
            try
            {
                LMLocationModel info = new LMLocationModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LMLocationModel>("Lmlocationgoods", "GetLMLocationInfoByCode", locationcode).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        private LMLocationModel GetLMLocationInfoByID(int locationid)
        {
            try
            {
                LMLocationModel info = new LMLocationModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LMLocationModel>("Lmlocationgoods", "GetLMLocationInfoByID", locationid).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }
        private bool UpdateLmlocationStock(DbTransaction tran, List<LMLocationModel> lstLMlocationInfo)
        {
            try
            {
                if (lstLMlocationInfo == null || lstLMlocationInfo.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("Lmlocationgoods", "UpdateLmlocationStock", lstLMlocationInfo);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        public bool UpdatelocationMaxStock(List<LMLocationModel> List)
        {
            var result = false;
            var message = string.Empty;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("Lmlocationgoods", "UpdateLmlocationStockByCode", List);
                }
                result = true;
                tran.Commit();
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                result = false;
                message = ex.Message;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return result;
        }

        public bool PostlocationGoodslist(int Change_ALOCATIONID, int Change_AGOODSID, int Change_BLOCATIONID, int Change_BGOODSID, int CREATEDAID)
        {
            var result = false;
            var log_result = false;
            var message = string.Empty;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                List<Pft_lmlocationgoodsLog> locationgoodsLog = new List<Pft_lmlocationgoodsLog>();
                locationgoodsLog.Add(new Pft_lmlocationgoodsLog { LOCATIONID = Change_ALOCATIONID, GOODSID = Change_AGOODSID, NEWGOODSID = Change_BGOODSID, CREATEDAID = CREATEDAID });
                locationgoodsLog.Add(new Pft_lmlocationgoodsLog { LOCATIONID = Change_BLOCATIONID, GOODSID = Change_BGOODSID, NEWGOODSID = Change_AGOODSID, CREATEDAID = CREATEDAID });
                List<LMLocationModel> list = new List<LMLocationModel>();
                LMLocationModel ALocation=GetLMLocationInfoByID(Change_ALOCATIONID);
                decimal AWaringStock = 0; decimal AMaxStock = 0;
                if (ALocation != null && ALocation.LOCATIONID != 0)
                {
                    AWaringStock = ALocation.WARNINGSTOCK;
                    AMaxStock = ALocation.MAXSTOCK;
                    //list.Add(ALocation);
                }
                LMLocationModel BLocation = GetLMLocationInfoByID(Change_BLOCATIONID);
                if (BLocation != null && BLocation.LOCATIONID != 0)
                {
                    if (AWaringStock!=decimal.Parse("0"))
                    {
                        ALocation.WARNINGSTOCK = BLocation.WARNINGSTOCK;
                        ALocation.MAXSTOCK = BLocation.MAXSTOCK;
                        BLocation.WARNINGSTOCK = AWaringStock;
                        BLocation.MAXSTOCK = AMaxStock;
                        list.Add(ALocation);
                        list.Add(BLocation);
                    }
                    
                }
                result = MB.RuleBase.Common.DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "Lmlocationgoods", "UpdateLmlocationGoods", Change_ALOCATIONID, Change_AGOODSID, Change_BLOCATIONID, Change_BGOODSID) > 0;
                result = result && UpdateLmlocationStock(tran, list);
                if (result)
                {
                    using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                    {
                        MB.Util.TraceEx.Write("记录货位搬移记录,信息数：" + locationgoodsLog.Count.ToString());
                        bulk.WriteToServer("Lmlocationgoods", "InsertlmlocationgoodsLog", locationgoodsLog);
                    }
                }
                log_result = true;
                if (log_result)
                {
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                result = false;
                message = ex.Message;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return result;
        }

        public bool bindProduceLabelLocation(int UserID, List<produce_eleclabellocationModel> list)
        {
            var result = false;
            var message = string.Empty;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                foreach (var item in list)
                {
                    item.CREATERID = UserID;
                }
                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("Lmlocationgoods", "bindLocationIDByLabelID", list);
                }
                result = true;
                tran.Commit();
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                result = false;
                message = ex.Message;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return result;
        }

        public produce_eleclabellocationModel checkLabelIsbind(string LabelID)
        {
            try
            {
                produce_eleclabellocationModel info = new produce_eleclabellocationModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<produce_eleclabellocationModel>("Lmlocationgoods", "checkLocationIDByLabelID", LabelID).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public bool DebindLabelLocationRelation(string LabelID)
        {
            try
            {
                bool info = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Lmlocationgoods", "DebindLocationIDByLabelID", LabelID)>0;
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        public producesaleorderbatchitemModel getProduceQtyInfo(string Producebatch)
        {
            try
            {
                producesaleorderbatchitemModel info = new producesaleorderbatchitemModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<producesaleorderbatchitemModel>("Lmlocationgoods", "GetProduceQTY", Producebatch).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public List<ProducePickSituation> getProducePickInfo(string Producebatch)
        {
            try
            {
                List<ProducePickSituation> info = new List<ProducePickSituation>();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ProducePickSituation>("Lmlocationgoods", "GetProduceSituation", Producebatch);
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public bool UpdateProducefinishQTY(producesaleorderbatchitemModel Postinfo)
        {
            try
            {
                bool info = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Lmlocationgoods", "UpdateProduceFinishQTY", Postinfo.FINISHQTY, Postinfo.CREATERID, Postinfo.PRODUCEBATCH, Postinfo.GOODSCODE) > 0;
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        public bool bindPFTLabelLocation(int UserID, List<pft_eleclabellocationModel> list)
        {
            var result = false;
            var message = string.Empty;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                foreach (var item in list)
                {
                    item.CREATERID = UserID;
                }
                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("Lmlocationgoods", "bindPFTLocationIDByLabelID", list);
                }
                result = true;
                tran.Commit();
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                result = false;
                message = ex.Message;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
            return result;
        }

        public pft_eleclabellocationModel checkPFTLabelIsbind(string LabelID)
        {
            try
            {
                pft_eleclabellocationModel info = new pft_eleclabellocationModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<pft_eleclabellocationModel>("Lmlocationgoods", "checkPFTLocationIDByLabelID", LabelID).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public bool DebindPFTLabelLocationRelation(string LabelID)
        {
            try
            {
                bool info = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Lmlocationgoods", "DebindPFTLocationIDByLabelID", LabelID) > 0;
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        public LMLocationModel GetPFTLMLocationInfoByCode(string locationcode)
        {
            try
            {
                LMLocationModel info = new LMLocationModel();
                info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LMLocationModel>("Lmlocationgoods", "GetPFTLMLocationInfoByCode", locationcode).FirstOrDefault();
                return info;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }
    }
}
